#include <string.h>
#include "uart1.h"
#include "gpio.h"

volatile UINT08 time_st=0;
volatile UINT08 qita_cmd=0x00;
volatile UINT08 USRT_TXDATA=0x00;

UINT08 MYUSART_1[RS485_RX_LEN];
UINT08 MYUSART_2[RS485_RX_LEN];
UINT08 RS485_TX_BUF[RS485_RX_LEN];

UINT08 CRC_lens=0;
UINT08 Crc8_load=0;

volatile UINT08 Is_interrupted=0x00;

volatile UINT08 CH1_8;  
volatile UINT08 CH9_16; 
volatile UINT08 CH17_24;
volatile UINT08 CH25_30;

UINT08 queuehead = 0;
UINT08 queuetail = 0;
UINT08 msg[QUEUELEN];

UINT08 queuehead_tx = 0;
UINT08 queuetail_tx = 0;
UINT08 msg_tx[QUEUELEN_tx];

void putMsg(UINT08 m)    
{
    if(queuetail>=QUEUELEN)
        queuetail=0;
    
    msg[queuetail] = m;
    if(++queuetail >= QUEUELEN) 
        queuetail = 0;
}

UINT08 getMsg(void)    
{
	
	UINT08 m = msg[queuehead];
	if(++queuehead >= QUEUELEN) queuehead = 0;
	return m;
	
}

UINT08 testQueue(void)
{
	return (queuetail == queuehead)?0:1;
}

void putMsg_tx(UINT08 m_tx)    
{
    if(queuetail_tx>=QUEUELEN_tx)
        queuetail_tx=0;
    
    msg_tx[queuetail_tx] = m_tx;
    if(++queuetail_tx >= QUEUELEN_tx) 
        queuetail_tx = 0;
}

UINT08 getMsg_tx(void)    
{
	UINT08 m_tx = msg_tx[queuehead_tx];
	if(++queuehead_tx >= QUEUELEN_tx) queuehead_tx = 0;
	return m_tx;
}

UINT08 testQueue_tx(void)
{
	return (queuetail_tx == queuehead_tx)?0:1;
}

/*******************************************************************************
 * 名称: UART1_SendString
 * 功能: UART1发送len个字符
 * 形参: data -> 指向要发送的字符串
 *       len -> 要发送的字节数
 * 返回: 无
 * 说明: 无 
 ******************************************************************************/
 
void UART1_SendString(UINT08* Data,UINT16 len)
{
UINT08 t;
        PF5 = 1;
		PFT5 = 0; 
	    Delay_Ms(3);
		for(t=0;t<len;t++)
		{
			while(TRMT1 !=1);
			TX1B=Data[t];
		}
		Delay_Ms(3);
		PF5 = 0;		                            
        PFT5 = 0; 									
}

UINT08 crc8_chack(UINT08 *message, UINT08 len)
{
	UINT08 crc;
	UINT08 i;
	crc = 0;
	while(len--)
	{
		crc ^= *message++;
		for(i = 0;i < 8;i++)
		{
			if(crc & 0x01)
			{
		          crc = (crc >> 1) ^ 0x8c;
			}
			else crc >>= 1;
		}
	}
	return crc; 
}

UINT08 CH1_8_old[3]={0x00,0x00,0x00};
UINT08 CH9_16_old[3]={0x00,0x00,0x00};
UINT08 CH17_24_old[3]={0x00,0x00,0x00};
UINT08 CH25_32_old[3]={0x00,0x00,0x00};
volatile  UINT08 tx_addr=0;
void Read_sock_s(void)
{
        CH1_8=0XFF;
        CH9_16=0XFF; 
        CH17_24=0XFF;
        CH25_30=0X3F;
               
        Read_open_lock();       
        CH1_8_old[0]=CH1_8;
        CH9_16_old[0]=CH9_16;
        CH17_24_old[0]=CH17_24;
        CH25_32_old[0]=CH25_30;
        
        Delay_Ms(5);
        CH1_8=0XFF;
        CH9_16=0XFF; 
        CH17_24=0XFF;
        CH25_30=0X3F;
        
        Read_open_lock();
        CH1_8_old[1]=CH1_8;
        CH9_16_old[1]=CH9_16;
        CH17_24_old[1]=CH17_24;
        CH25_32_old[1]=CH25_30;
        
        Delay_Ms(5);
        CH1_8=0XFF;
        CH9_16=0XFF; 
        CH17_24=0XFF;
        CH25_30=0X3F;
        
        Read_open_lock();
        CH1_8_old[2]=CH1_8;
        CH9_16_old[2]=CH9_16;
        CH17_24_old[2]=CH17_24;
        CH25_32_old[2]=CH25_30;

        if( CH1_8_old[0]==CH1_8_old[1])
        {
            CH1_8=CH1_8_old[0];    
        }
        else if( CH1_8_old[1]==CH1_8_old[2] )
        {
            CH1_8=CH1_8_old[1];    
        }
        else if( CH1_8_old[2]==CH1_8_old[0])
        {
            CH1_8=CH1_8_old[2];      
        }
        else 
        {
            CH1_8=CH1_8_old[1];
        }

        if( CH9_16_old[0]==CH9_16_old[1])
        {
            CH9_16=CH9_16_old[0];    
        }
        else if( CH9_16_old[1]==CH9_16_old[2] )
        {
            CH9_16=CH9_16_old[1];  
        }
        else if( CH9_16_old[2]==CH9_16_old[0])
        {
            CH9_16=CH9_16_old[2];  
        }
        else 
        {
            CH9_16=CH9_16_old[1];
        }

        
        if( CH17_24_old[0]==CH17_24_old[1] )
        {
            CH17_24=CH17_24_old[0];         
        }
        else if( CH17_24_old[1]==CH17_24_old[2])
        {
            CH17_24=CH17_24_old[1];        
        }
        else if( CH17_24_old[0]==CH17_24_old[2])
        {
            CH17_24=CH17_24_old[2];
        }
        else 
        {
            CH17_24=CH17_24_old[1];
        }
        
        
        if( CH25_32_old[0]==CH25_32_old[1] )
        {
            CH25_30=CH25_32_old[0];      
        }
        else if( CH25_32_old[1]==CH25_32_old[2] )
        {
            CH25_30=CH25_32_old[1];        
        }
        else if( CH25_32_old[0]==CH25_32_old[2])
        {
            CH25_30=CH25_32_old[2];
        }
       else 
       {
            CH25_30=CH25_32_old[1];
       }      
	  
}
unsigned  char bit_number;

static void mymove_bit(void)
{
static unsigned  char ua1=0;
static unsigned  char b1=0;
   ua1=0x00;
   ua1=MYUSART_2[5] & 0xFF;

   if(ua1 != 0xFF)
   {
		bit_number=0;
		for(b1=1;b1<=8;b1++)
		{
			bit_number= ua1 & 0x01;
			if(bit_number==0x01)
			{
			Read_channes1_8state(b1,1);   	
			}
			else 
			{
			Read_channes1_8state(b1,0);
			}
			ua1=ua1>>1;
		}
		Delay_Ms(20);
		PJ0 = 1;
		PF0 = 1;
		PF2 = 1;
		PF4 = 1;
		PJ3 = 1;
		PE5 = 1;
		PG5 = 1;
		PD1 = 1;
   }
   
   ua1=0x00;
   ua1=MYUSART_2[6] & 0xFF;
   if(ua1  != 0xFF)
   {
		bit_number=0;
		for(b1=1;b1<=8;)
		{
			bit_number= ua1 & 0x01;
			if(bit_number==0x01)
			{
			  Read_channes9_16state(b1,1); 	
			}
			else if(bit_number==0x00)
			{
			  Read_channes9_16state(b1,0);
			}
			ua1=ua1>>1;
			b1++;
		}
		Delay_Ms(20);
		PD3 = 1;
		PD5 = 1;
		PD7 = 1;
		PJ5 = 1;
		PJ7 = 1;
		PC1 = 1;
		PC3 = 1;
		PC5 = 1;
   }
}
static void move_bit(void)
{
static  unsigned char a1=0,b1=0;
   
   a1=0x00;
   a1=MYUSART_2[7] & 0xff;
   if( a1 !=0XFF)
   {
	   bit_number=0;
	   for(b1=1;b1<=8;b1++)
	   {
		   bit_number= a1&0x01;
	   		if(bit_number==0x01)
	   		{
				Read_channes17_24state(b1,1);	   	
			}
			else 
			{
				Read_channes17_24state(b1,0);
			}	    
   		a1=a1>>1;
	   }
	   	Delay_Ms(20);
	    PC7 = 1;
		PB1 = 1;
		PH0 = 1;
		PH2 = 1;
		PB3 = 1;
		PB5 = 1;
		PB7 = 1;
		PA1 = 1;
   }
   
   a1=0x00;
   a1=MYUSART_2[8];
   if(a1 & 0x3F !=0x3F)
   {
	   bit_number=0;
	   for(b1=1;b1<=6;b1++)
	   {
		   bit_number= a1&0x01;
	   		if(bit_number==0x01)
	   		{
				Read_channes25_30state(b1,1);   	
			}
			else 
			{
				Read_channes25_30state(b1,0);
			}
   		a1=a1>>1;
	   }
   }
   Delay_Ms(20);
   PA3 = 1;
   PA5 = 1;
   PA7 = 1;
   PH5 = 1;
   PH7 = 1;
   PG1 = 1;
}

sbit  asd_1;

void RS485_Data_parsing(void)
{

  UINT08 t=0;


  RS485_TX_BUF[0]=0XAA;
  RS485_TX_BUF[1]=0X55;
  RS485_TX_BUF[3]=Plate_address1;
  __Asm  CWDT;
  switch(MYUSART_2[4])
  {
     case 0x13:
      {
       RS485_TX_BUF[3]=Plate_address1;
       RS485_TX_BUF[4]=0X13;
        __Asm  CWDT;
		mymove_bit();
		move_bit();
        
        Read_sock_s();
        
        
        
        
        
        
        
        
        
        
        
        
        
        RS485_TX_BUF[0]=0XAA;
        RS485_TX_BUF[1]=0X55;
        RS485_TX_BUF[2]=0X06;
        RS485_TX_BUF[3]=Plate_address1;
        RS485_TX_BUF[4]=0X13;
		RS485_TX_BUF[5]=CH1_8;
		RS485_TX_BUF[6]=CH9_16;
		RS485_TX_BUF[7]=CH17_24;
		RS485_TX_BUF[8]=CH25_30;
		/*
        RS485_TX_BUF[5]=MYUSART_2[5];
        RS485_TX_BUF[6]=MYUSART_2[6];
        RS485_TX_BUF[7]=MYUSART_2[7];
        RS485_TX_BUF[8]=MYUSART_2[8];
		*/
        RS485_TX_BUF[9]=crc8_chack(RS485_TX_BUF,9);
		__Asm  CWDT;
        for(t=0;t<10;t++)
        {
          putMsg_tx(RS485_TX_BUF[t]);
          RS485_TX_BUF[t]=0;
        }
      break;
      }
      case 0x51:
      {        
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
     
       CH1_8=0XFF;  
       CH9_16=0XFF; 
       CH17_24=0XFF;
       CH25_30=0X3F;
	   __Asm  CWDT;
       Read_sock_s();
       RS485_TX_BUF[0]=0XAA;
       RS485_TX_BUF[1]=0X55;
       RS485_TX_BUF[2]=0X06;
       RS485_TX_BUF[3]=Plate_address1;
       RS485_TX_BUF[4]=0X51;
       RS485_TX_BUF[5]=CH1_8;
       RS485_TX_BUF[6]=CH9_16;
       RS485_TX_BUF[7]=CH17_24;
       RS485_TX_BUF[8]=CH25_30;
       
       RS485_TX_BUF[9]=crc8_chack(RS485_TX_BUF,9);
	   __Asm  CWDT;
		for(t=0;t<9;t++)
        {
          putMsg_tx(RS485_TX_BUF[t]);
          RS485_TX_BUF[t]=0;
        }		
		USRT_TXDATA=0xFF;
      break;
      }
	default:break;
  }
}

/*******************串口相关寄存器校验*********************
函 数 名：  void Uart_Init(void)                     
功能描述：  特殊功能寄存器校验                           
输入参数：  无                                           
函数返回值：无                                           
***********************************************************/
void Uart_Init(void)
{
	PFT7 = 0;		    
    PFT6 = 1;		    

    BR1R=84;	        
    BRGH1=1;             

    TX1M = 0;           
    RX1M = 0;           

    RX1EN = 1;          
    TX1EN = 1;          

	
    RX1IE = 1;          

	GIE = 1;            
}

/*******************Uart接收中断处理*********************
函 数 名：  void Uart1_Recv(void)                         *
功能描述：  Uart接收中断处理                              *
输入参数：  无                                           *
函数返回值：无                                           *
**********************************************************/
UINT08 stemp2=0;
UINT08 fghj=0;
UINT08 RSlen1=0;
UINT08 jk=0;
void Uart1_Recv(void) 
{  
    UINT08   err_flag;
    UINT08   Res;
	__Asm  CWDT;
    if(FERR1)               
	     err_flag = 1; 
    else       
	     err_flag = 0; 

    Res = RX1B;              
	
    if(OERR1)            
    {
        err_flag=1; 
		RX1EN = 0;       
		RX1EN = 1;
    }
	
    if(err_flag == 1)   
    {
		fghj=0;
		stemp2=0;
		RSlen1=0;
        return;
    }
	
	
    if(stemp2>=0x03)
     {
        MYUSART_1[fghj++]=Res;
        if(fghj>=(RSlen1+4))
         {       
           
		   __Asm  CWDT;
           if(MYUSART_1[4]==0x13 && MYUSART_1[3]==Plate_address1)
                    {
                      for(jk=0;jk<10;jk++)
                      {
                         putMsg(MYUSART_1[jk]);
                         MYUSART_1[jk]=0x00;
                      }
                      time_st++;
                    }
 
           else if(MYUSART_1[4]==0x54 && MYUSART_1[3]==Plate_address1)
                    {
						
                      if(MYUSART_1[5]==0x00)
                      {
                       
                      }
                      else
                      {
                       
                      }
					  
                    for(jk=0;jk<7;jk++)
                        {                    
                          MYUSART_1[jk]=0x00;
                        }
                    }

                    else
                    {
                      if(MYUSART_1[3]==Plate_address1)
                      {
                      for(jk=0;jk<RS485_RX_LEN;jk++)
                          {                        
                            MYUSART_2[jk]=MYUSART_1[jk];
                          }
                      qita_cmd=0xff;
                      }
                      for(jk=0;jk<RS485_RX_LEN;jk++)
                        {                    
                          MYUSART_1[jk]=0x00;
                        }
                    }
           fghj=0;
           stemp2=0;
           RSlen1=0;
         }
      }
	  
    if(stemp2==0x02)
     {
       RSlen1=Res;
       MYUSART_1[fghj++]=Res;
       stemp2=0x03;
     }
	 
   if(Res==0xAA && stemp2==0x00)
    {
      stemp2=0x01;
      MYUSART_1[fghj++]=0xAA;
    }
	
    else if(Res==0x55 && stemp2==0x01)
    {
      stemp2=0x02;
      MYUSART_1[fghj++]=0x55;
    }
}
/*************Uart1发送中断处理函数*************************
函 数 名：Uart1_Tx                                        *
功能描述：Uart1发送中断函数                                *
输入参数：  无                                           *
函数返回值：无                                           *
**********************************************************/
UINT08 c_msg=0;
UINT08 TX_DATA1=0;
void Uart1_Tx(void) 
{
	Is_interrupted=0xff;
   if(testQueue_tx())
          {
		    __Asm  CWDT;
			TX_DATA1=getMsg_tx();
            TX1B=TX_DATA1;      
          }
    else
    {
			TX1IE = 0;          
			while(GIE == 1)     
			{
				GIE = 0;
			}
			
			while(TRMT1 !=1);
			
			__Asm  CWDT;
			Is_interrupted=0x00;
			T16G1IE=0;
			USRT_TXDATA=0x00;
			for(c_msg=0;c_msg<QUEUELEN_tx;c_msg++)
			{
			msg_tx[c_msg]=0x00;    
			}
			queuehead_tx = 0;
			queuetail_tx = 0;
            
			for(c_msg=0;c_msg<QUEUELEN;c_msg++)
			{
			msg[c_msg]=0x00;      
			}
			queuehead= 0;
			queuetail= 0;
          
			fghj=0;
			RSlen1=0;
			stemp2=0x00;
			time_ov3=0x00;
			tim3_tim=0;
			Uart_Flag = 0xFF;
    }
}

/*
AA 55 04 02 13 00 00 80
AA 55 04 02 13 00 01 DE

AA 55 04 02 13 01 00 44
AA 55 04 02 13 01 01 1A
*/
